﻿/*
 * Copyright 2007-2008  Lukasz Laszko lukaszlaszko@gmail.com
 * 
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that the copyright notice and this
 * permission notice and warranty disclaimer appear in supporting
 * documentation, and that the name of the author not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * 
 * The author disclaim all warranties with regard to this
 * software, including all implied warranties of merchantability
 * and fitness.  In no event shall the author be liable for any
 * special, indirect or consequential damages or any damages
 * whatsoever resulting from loss of use, data or profits, whether
 * in an action of contract, negligence or other tortious action,
 * arising out of or in connection with the use or performance of
 * this software. 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Pg.BioMedics.SDR.Web.Controls
{
    /// <summary>
    /// Holds information about uploaded file
    /// </summary>
    public class UploadedFile : IDisposable
    {
        #region Constructors

        /// <summary>
        /// Creates Uploadedfile instance
        /// </summary>
        internal UploadedFile()
        {
            Location = Path.GetTempFileName();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets / sets file name
        /// </summary>
        public string FileName
        {
            get;
            internal set;
        }

        /// <summary>
        /// Gets / sets temprary location of this file
        /// </summary>
        public string Location
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets / sets position in the file
        /// </summary>
        public int Position
        {
            get;
            internal set;
        }

        /// <summary>
        /// Gets / sets file length
        /// </summary>
        public int Length
        {
            get;
            internal set;
        }

        /// <summary>
        /// Defines if the upload file is ready
        /// </summary>
        public bool Ready
        {
            get
            {
                return (Position == Length);
            }
        }

        /// <summary>
        /// Gets file stream related with given uploaded file.
        /// Stream is available only if file is ready
        /// </summary>
        public Stream FileStream
        {
            get
            {
                if (Ready)
                    return File.OpenRead(Location);
                else
                    return null;
            }
        }

        public byte[] Data
        {
            get
            {
                if (Ready)
                    return File.ReadAllBytes(Location);
                else
                    return null;
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            try
            {
                if (File.Exists(Location))
                    File.Delete(Location);
            }
            catch { }
        }

        #endregion
    }
}
